package org.cying.main.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import org.cying.main.dao.CreateTask;
import org.cying.main.dao.DeleteTask;
import org.cying.main.dao.QueryTask;
import org.cying.main.dao.UpdateTask;
import org.cying.main.domain.Task;
import org.cying.main.domain.CommitContent;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务服务接口实现类
 * @author Cying
 */
@Service
public class TaskService {

    @Resource
    private QueryTask query;
    
    @Resource
    private UpdateTask update;
    
    @Resource
    private CreateTask create;
    
    @Resource
    private DeleteTask delete;

    public boolean issueTask(
        String subject, String describe, Long issuer, String deadLine, Integer type,
        List<String> deptList, List<String> groupList, List<String> stuList, List<String> imgList
    ) {
        String uid = IdUtil.fastSimpleUUID();
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("subject", subject);
        map.put("describe", describe);
        map.put("issuer", issuer);
        map.put("pubDate", DateUtil.today());
        map.put("deadLine", deadLine);
        map.put("type", type);
        map.put("deptList", deptList);
        map.put("groupList", groupList);
        map.put("stuList", stuList);
        map.put("imgList", imgList);
        System.out.println(map);
        int f1 = create.issueTask(map);
        if (f1 == 1) {
            create.issueImg(map);
            create.issueOrg(map);
        }
        return f1 == 1;
    }

    public int commitContent(String content, String tid, Long sid) {
        Map<String, Object> map = new HashMap<>();
        map.put("tid", tid);
        map.put("sid", sid);
        map.put("content", content);
        return update.commitContent(map);
    }

    public int commitImgs(String tid, Long sid, List<String> imgs) {
        Map<String, Object> map = new HashMap<>();
        map.put("tid", tid);
        map.put("sid", sid);
        map.put("imgs", imgs);
        return create.commitImgs(map);
    }

    public int deleteImgs(String tid, Long sid) {
        Map<String, Object> map = new HashMap<>();
        map.put("tid", tid);
        map.put("sid", sid);
        return delete.deleteImgs(map);
    }

    public int setClose(String tid) {
        return update.setClose(tid);
    }
    
    public Integer getTaskTotal(Map<String, Object> map) {
        return query.getTaskTotal(map);
    }
    
    public Integer getTaskFinish(Map<String, Object> map) {
        return query.getTaskFinish(map);
    }
    
    public Integer getTaskUnFinish(Map<String, Object> map) {
        return query.getTaskUnFinish(map);
    }
    
    public Integer getTaskPreFinish(Map<String, Object> map){
        return query.getTaskPreFinish(map);
    }

    /**
     * 从数据库获取任务
     * @param sid 学号，为空时不进行学号筛选
     * @param dept 部门，为空时不进行部门筛选
     * @param close 是否截止
     * @param issuer 发布者，为空时不进行发布者筛选
     * @param complete 是否完成
     * @param type 类型，0 - 部门型任务，1 - 小组型任务，2 - 个人型任务
     * @return 查询结果
     */
    public List<Task> getTask(Long sid, String dept, Integer close, Long issuer, Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("sid", sid);
        map.put("dept", dept);
        map.put("close", close);
        map.put("issuer", issuer);
        map.put("type", type);
        System.out.println(map);
        return query.getTask(map);
    }

    /**
     * 从数据库分别获取学生提交的任务内容、图片列表，并封装成 CommitContent 返回
     * 其中，如果 context 为 null，imgList 的长度为 0，则判定为该学生未完成任务
     * @param sid 学号
     * @param tid 任务号
     * @return 封装完成的 CommitContent 对象
     */
    public CommitContent getCommited(Long sid, String tid) {
        Map<String, Object> map = new HashMap<>();
        map.put("tid", tid);
        map.put("sid", sid);
        CommitContent result = new CommitContent();
        if (query.getCheck(map) == 0)
            return null;
        result.setContext(query.getCommitedContext(map));
        result.setImgs(query.getCommitedImages(map));
        int flag = (result.getContext() == null && result.getImgs().isEmpty()) ? 0 : 1;
        result.setFlag(flag);
        return result;
    }

    public boolean deleteTask(String tid, Long sid, String department, String group) {
        Map<String, Object> map = new HashMap<>();
        map.put("tid", tid);
        map.put("sid", sid);
        map.put("department", department);
        map.put("group", group);
        System.out.println(map);
//        delete.deleteImgs(map);
//        delete.deleteStatus(map);
//        return delete.deleteTask(map) > 0;
        return true;
    }

}


